package com.hotel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotel.entity.Dish;
import com.hotel.exception.BusinessException;
import com.hotel.mapper.DishMapper;
import com.hotel.response.ResultCode;
import com.hotel.service.AliOssService;
import com.hotel.service.CategoryService;
import com.hotel.service.DishService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hotel.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 菜品管理 服务实现类
 * </p>
 *
 * @author Lin
 * @since 2022-05-30
 */
@Service
public class DishServiceImp extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Resource
    private CategoryService categoryService;

    @Override
    public IPage<Dish> getDishList(String id, Integer level, String name, Integer status, Integer current, Integer size) {
        Page<Dish> dishPage = new Page<>(current, size);
        IPage<Dish> dishIPage = null;
        QueryWrapper<Dish> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(id)&&level!=null){
            if(level==1){
                List<Object> categoryId = categoryService.getCategoryId(id);
                if(categoryId.size()>1){
                    wrapper.in("category_id",categoryId);
                }else {
                    wrapper.eq("category_id",id);
                }
            }else if(level==2){
                wrapper.eq("category_id",id);
            }
        }
        if (StringUtils.isNotEmpty(name)){
            wrapper.like("name",name);
        }
        if(status!=null){
            wrapper.eq("status",status);
        }
        wrapper.orderByAsc("sort");
        dishIPage=page(dishPage,wrapper);
        return dishIPage;
    }

    @Override
    public int deleteDishById(String id) {
        return this.baseMapper.deleteById(id);
    }

    @Resource
    private AliOssService aliOssService;

    public String upload(MultipartFile file, String pathName) {
        return this.aliOssService.upload(file,pathName);
    }

    @Override
    public int addDish(DishVO dishVO) {
        QueryWrapper<Dish> wrapper = new QueryWrapper<>();
        wrapper.eq("name",dishVO.getName());
        List<Dish> dishes = this.baseMapper.selectList(wrapper);
        if(dishes.size()>0){
            throw new BusinessException(ResultCode.DISHES_ALREADY_EXIST.getCode(),
                    ResultCode.DISHES_ALREADY_EXIST.getMessage());
        }
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishVO,dish);
        dish.setStatus(1);
        dish.setSale(0);
        dish.setCreateUser(1L);
        dish.setUpdateUser(1L);
        dish.setIsDeleted(0);
        return this.baseMapper.insert(dish);
    }

    @Override
    public Dish editDish(String id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public int updateDish(DishVO dishVO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishVO,dish);
        return this.baseMapper.updateById(dish);
    }

    @Override
    public int updateStatus(Long id,Integer status) {
        Integer newStatus = (status==1?0:1);
        Dish dish = new Dish().setStatus(newStatus).setId(id);
        return this.baseMapper.updateById(dish);
    }

}
